package com.yanqu.road.server.cmd.activity.snowgarden;

import com.yanqu.road.entity.activity.snowgarden.config.SnowGardenConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SnowGardenProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.snowgarden.SnowGardenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.*;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = GameProtocol.S_SNOW_GARDEN_CROSS_VISIT, desc = "请求冰雪游园会拜访")
public class SnowGardenCrossVisitCmd extends AbstractCommand {
    private int activityId;
    private long userId;   // 来拜访的玩家id
    private long userServerId;   // 来拜访的玩家服务器id
    private int visitType; // 拜访类型
    private String visitParam; // 拜访参数
    private long targetUserId;   // 拜访目标玩家id
    private long targetServerId;   // 拜访目标玩家服务器id
    private long recordTime;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SnowGardenProto.SnowGardenToCrossVisitReqMsg reqMsg = SnowGardenProto.SnowGardenToCrossVisitReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = reqMsg.getUserId();
        visitType = reqMsg.getVisitType();
        visitParam = reqMsg.getVisitParam();
        userServerId = reqMsg.getUserServerId();
        targetUserId = reqMsg.getTargetUserId();
        targetServerId = reqMsg.getTargetServerId();
        recordTime = reqMsg.getRecordTime();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SnowGardenConfig config = SnowGardenMgr.getSnowGardenConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_HAD_END;
            return;
        }
        ret = SnowGardenMgr.visitPlayerEx(config.getActivityInfo().getActivityId(), userId, userServerId, visitType, visitParam, targetUserId, targetServerId, recordTime);
//        GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(targetUserId);
//        if (onlinePlayer != null) {
//            ret = player.getModule(SnowGardenModule.class).crossVisit(userId, userServerId, visitType, visitParam, targetUserId, targetServerId);
//        } else {// 玩家不在线
//            UserBaseInfo userBase = UserMgr.getUserBaseInfo(targetUserId, targetServerId);
//            if(userBase == null){userBase = new UserBaseInfo();}
//            SnowGardenUserData userData;
//                    String result;
//            synchronized (this) {
//                userData = SnowGardenMgr.getUserData(activityId,targetUserId);
//                SnowGardenVisitorRecordData recordData = SnowGardenMgr.getUserVisitData(activityId, targetUserId, eSnowGardenVisitType.VISIT_TYPE_VISITED_ME.getType());
//                result = SnowGardenMgr.visitPlayer(userId, targetUserId, visitType, visitParam, userData, recordData);
//            }
//            SnowGardenProto.SnowGardenToCrossVisitRespMsg.Builder builder = SnowGardenProto.SnowGardenToCrossVisitRespMsg.newBuilder();
//            builder.setActivityId(activityId);
//            builder.setRet(0);
//            builder.setUserId(userId);
//            builder.setUserServerId(userServerId);
//            builder.setUserData(PlayerBasePb.parsePlayerBaseTempMsg(userBase));
//            SnowGardenProto.SnowGardenUserDataMsg.Builder userBuilder = SnowGardenPb.parseSnowGardenUserData(userData);
//            builder.setUserActData(userBuilder);
//            builder.setVisitResult(result);
//            builder.setVisitedUserId(targetUserId);
//            YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C_SNOW_GARDEN_VISITED_RESP, builder);
//            GamePlayerMgr.sendPacket(0, message);
//        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret > 0) {
            SnowGardenProto.SnowGardenToCrossVisitRespMsg.Builder builder = SnowGardenProto.SnowGardenToCrossVisitRespMsg.newBuilder();
            builder.setActivityId(activityId);
            builder.setRet(ret);
            builder.setUserId(userId);
            builder.setUserServerId(userServerId);
            YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C_SNOW_GARDEN_VISITED_RESP, builder);
            GamePlayerMgr.sendPacket(0, message);
        }
    }
}
